package ui;

import java.awt.Color;

import utils.UIColors;

import mainpackage.CategoryNameNotUniqueException;
import mainpackage.DataManager;
import mainpackage.NoColorsLeftException;

public class AddCategoryPresenter {

	DataManager dataManager;
	AddCategoryWindow addCategoryWindow;
	MainPresenter mainPresenter;

	public AddCategoryPresenter(AddCategoryWindow addCategory) {
		this.addCategoryWindow = addCategory;
	}

	public void saveButtonClicked() {
		String categoryName = addCategoryWindow.getCategoryName();
		try {
			if (categoryName == null || categoryName.equals("")) {
				addCategoryWindow.getTitleField().setBackground(UIColors.fieldHighlightColor);
			} else {
				dataManager.addCategory(categoryName);
				mainPresenter.addCategoryToList(dataManager
						.getCategoryByName(categoryName));
				addCategoryWindow.dispose();
			}
		} catch (NoColorsLeftException e) {
			// shouldn't ever get here
			e.printStackTrace();
		} catch (CategoryNameNotUniqueException e) {
			addCategoryWindow.getTitleField().setBackground(UIColors.fieldHighlightColor);
		}

	}

	public void setDataManager(DataManager dataManager) {
		this.dataManager = dataManager;
	}

	public void setMainPresenter(MainPresenter mainPresenter) {
		this.mainPresenter = mainPresenter;
	}

}
